<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="documentName">
              <el-input v-model="filters.documentName" clearable placeholder="文档名称"></el-input>
            </el-form-item>
            <HospitalSelect @hospitalId="getHospitalId" :id="filters.hospitalId" :site="2"></HospitalSelect>
            <InstitutionSelect @institutionId="getInstitutionId" :id="filters.institutionId" :site="2"></InstitutionSelect>
            <el-form-item prop="state">
              <el-select v-model="filters.state" clearable placeholder="状态">
                <el-option v-for="item in StateList" :key="item.value" :label="item.lable" :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="dataList" :fit="true" highlight-current-row>
        <!-- <el-table-column prop="documentId" label="文档编号" align="center" width="120"></el-table-column > -->
        <el-table-column prop="documentName" label="文档名称" width="200" align="center"></el-table-column>
        <el-table-column prop="documentType" label="文档类型" :formatter="typeFormat" width="150" align="center"></el-table-column>
        <el-table-column prop="firstClassifyName" label="一级分类" width="120"></el-table-column>
        <el-table-column prop="classifyName" label="二级分类" width="120"></el-table-column>
        <el-table-column prop="institutionName" label="机构名称" width="200"></el-table-column>
        <el-table-column prop="hospitalName" label="医院名称" width="180"></el-table-column>
        <el-table-column label="文档状态" align="center" width="100">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" :type="documentStateType(scope.row.documentState)">
                {{ documentStateFormat(scope.row.documentState) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <!-- prop="state":formatter="stateFormat"   -->
        <el-table-column prop="createUserName" label="创建人姓名" align="center" width="130"></el-table-column>
        <el-table-column label="状态" align="center" width="100">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" :type="stateType(scope.row.state)" effect="dark">
                {{ stateFormat(scope.row.state) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间"></el-table-column>

        <!-- <el-table-column prop="modifyUserName" label="修改人姓名"></el-table-column>
        <el-table-column prop="modifyDate" label="修改人时间"></el-table-column> -->

        <el-table-column label="操作" width="200" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button v-has="permissionSearch" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <!--  -1.删除 0.禁用 1.启用-->
                <el-dropdown-item v-has="permissionEdit" v-if="scope.row.state != -1" @click.native="handleClickEditor(scope.row)">编 辑</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" v-if="scope.row.state === 1" @click.native="handleUpdateState(scope.row, 0)">禁 用</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" v-if="scope.row.state === 0" @click.native="handleUpdateState(scope.row, 1)">启 用</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" v-if="scope.row.state === 0" @click.native="handleUpdateState(scope.row, -1)">删 除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import DocumentApi from '@/api/document/document';
import * as message from '@/constants/message';
import HospitalSelect from '@/components/HospitalSelect';
import InstitutionSelect from '@/components/InstitutionSelect';

export default {
  name: 'DocumentInfoManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      StateList: [
        { lable: '禁用', value: 0 },
        { lable: '启用', value: 1 },
        { lable: '删除', value: -1, disabled: true },
      ],
      filters: {
        state: '',
        documentName: '',
        hospitalId: '',
        institutionId: '',
      },

      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      permissionAdd: 'SYSTEM_MANAGER_DOCUMENT_INFO_LIST_ADD', //添加
      permissionSearch: 'SYSTEM_MANAGER_DOCUMENT_INFO_LIST_SEE', // 查看
      permissionEdit: 'SYSTEM_MANAGER_DOCUMENT_INFO_LIST_EDIT', // 编辑
      permissionDelete: 'SYSTEM_MANAGER_DOCUMENT_INFO_LIST_DELETE', // 删除
    };
  },
  components: {
    HospitalSelect,
    InstitutionSelect,
  },
  watch: {
    //to去哪，from从哪来
    $route(to, from) {
      if (from.path == '/document/documentInfoEdit' || from.path == '/document/documentInfoAdd') {
        this.getData();
      }
    },
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    if (this.$route.meta.keepAlive) {
      this.getData();
    }
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      this.getData();
      this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },


    getData() {
      // 获取列表数据
      let _this = this;
      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        state: _this.filters.state,
        institutionId: _this.filters.institutionId,
        hospitalId: _this.filters.hospitalId,
        documentName: _this.filters.documentName,
      };
      DocumentApi.listDocumentinfo(cond).then((res) => {
        _this.dataList = res.data.list;
        _this.page.pageTotal = res.data.total;
      });
    },
    resetForm() {
      // 重置
      this.$refs.searchForm.resetFields();
      this.init();
    },
    onSearch() {
      // 搜索
      this.page.pageNo = 1;
      this.getData();
    },
    getHospitalId(val) {
      this.filters.hospitalId = String(val);
    },
    getInstitutionId(val) {
      this.filters.institutionId = String(val);
    },
    typeFormat(row, column, cellValue) {
      if (cellValue == 1) {
        return '医院文档';
      } else if (cellValue == 2) {
        return '医检文档';
      } else if (cellValue == 3) {
        return '系统文档';
      }
    },
    documentStateFormat(cellValue) {
      if (cellValue == 0) {
        return '待处理';
      } else if (cellValue == 1) {
        return '已处理';
      } else if (cellValue == 2) {
        return '已回复';
      }
    },
    documentStateType(cellValue) {
      if (cellValue == 0) {
        return 'warning';
      } else if (cellValue == 1) {
        return 'success';
      } else if (cellValue == 2) {
        return '';
      }
    },
    stateFormat(cellValue) {
      if (cellValue == 1) {
        return '启用中';
      } else if (cellValue == 0) {
        return '已禁用';
      } else if (cellValue == -1) {
        return '已删除';
      }
    },
    stateType(cellValue) {
      if (cellValue == 1) {
        return 'success';
      } else if (cellValue == 0) {
        return 'danger';
      } else if (cellValue == -1) {
        return 'info';
      }
    },
    handleClickAdd() {
      let _this = this;
      _this.$router.push('/document/documentInfoAdd');
    },
    handleClickEditor(row) {
      // 编辑
      let _this = this;
      _this.$router.push({
        path: '/document/documentInfoEdit',
        query: { id: row.documentId },
      });
    },
    handleClickDetail(row) {
      // 查看详情
      let _this = this;
      _this.$router.push({
        path: '/document/documentInfoDetail',
        query: { id: row.documentId },
      });
    },
    handleUpdateState(row, state) {
      // 修改所有状态
      let _this = this;
      let cond = {
        id: row.documentId,
        state: state,
      };
      let tit = '';
      if (state == 1) {
        tit = '禁用';
      } else if (state == 0) {
        tit = '启用';
      } else if (state == -1) {
        tit == '删除';
      }
      _this.$confirm('确认' + tit + '该文档？', message.TITLE_PROMPT).then(() => {
        DocumentApi.updateDocumentinfo(cond).then((res) => {
          this.$Utils.notifySuccess();
          this.getData();
        });
      });
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.getData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.getData();
    },
  },
};
</script>

<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
